package com.ldxiwxr.controller.controller;

import com.ldxiwxr.common.common.Result;
import com.ldxiwxr.system.query.ForgetPasswordQuery;
import com.ldxiwxr.system.query.LoginQuery;
import com.ldxiwxr.system.query.RegisterQuery;
import com.ldxiwxr.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * 登录页面具体功能控制类
 * @author SuBanxia
 */
@RestController
@Api(tags = "/LoginController")
public class LoginController {

    @Resource
    private IUserService userService;

    /**
     * 登录功能实现
     * @param loginQuery 登陆时查询内容
     * @param request 请求
     */
    @ApiOperation("登录功能")
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginQuery loginQuery, HttpServletRequest request){
        return userService.login(loginQuery.getUsername(), loginQuery.getPassword(), loginQuery.getCode(), loginQuery.getIsRem(), request);
    }

    /**
     * 忘记密码登录
     * @param forgetPasswordQuery 忘记密码时查询的内容
     * @return 结果
     */
    @ApiOperation("忘记密码时登录功能")
    @PostMapping("/loginForgetPassword")
    public Result<?> loginForgetPassword(@RequestBody ForgetPasswordQuery forgetPasswordQuery){
        return userService.loginByForgetPwd(forgetPasswordQuery.getPhone(), forgetPasswordQuery.getCode(), forgetPasswordQuery.getPassword());
    }

    /**
     * 注册功能
     * @param registerQuery 注册查询内容
     * @return 结果
     */
    @ApiOperation("注册功能")
    @PostMapping("/register")
    public Result<?> register(@RequestBody RegisterQuery registerQuery){
        return userService.register(registerQuery.getUsername(), registerQuery.getPhone(), registerQuery.getCode(), registerQuery.getPassword());
    }

    /**
     * 登出功能实现
     */
    @ApiOperation("登出功能")
    @PostMapping("/logout")
    public Result<?> logout(){
        return userService.logout();
    }
}
